#include "chatdialog.h"
#include "ui_chatdialog.h"
#include "clickedbtn.h"
#include "chatuserwid.h"
#include "chatuserlist.h"
#include "loadingdialog.h"

#include <QRandomGenerator>
#include <QListWidgetItem>
#include <QThread>
#include <QTimer>

ChatDialog::ChatDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::ChatDialog)
    , _menu_mode(ChatUIMode::ChatMode)
    , _state(ChatUIMode::ChatMode)
    , _b_loading(false)
{
    ui->setupUi(this);
    ui->add_btn->SetState("normal", "hover", "press");
    ShowSearch(false);
    //临时测试使用
    TestAddChatUserList();
    _SetConnect();
}

void ChatDialog::ShowSearch(bool bSearch)
{
    if(bSearch) // 如果是搜索模式，则显示查询窗口
    {
        qDebug("查询窗口");
        ui->search_list_widget->show();
        ui->user_list_Widget->hide();
        ui->con_user_list_widget->hide();
        _menu_mode = ChatUIMode::SearchMode;
    }
    else if(_state == ChatUIMode::ChatMode)
    {
        qDebug("聊天窗口");
        ui->user_list_Widget->show();
        ui->search_list_widget->hide();
        ui->con_user_list_widget->hide();
        _menu_mode = ChatUIMode::ChatMode;
    }
    else if(_state == ChatUIMode::SearchMode)
    {
        qDebug("联系人窗口");
        ui->con_user_list_widget->show();
        ui->user_list_Widget->hide();
        ui->search_list_widget->hide();
        _menu_mode = ChatUIMode::ContactMode;
    }
}

ChatDialog::~ChatDialog()
{
    delete ui;
}

void ChatDialog::_SetConnect()
{
    // 加载用户列表
    connect(ui->user_list_Widget, &ChatUserList::sig_loading_chat_user,
            this, &ChatDialog::slot_loading_chat_user);
}

void ChatDialog::slot_loading_chat_user()
{
    if(_b_loading)
    {
        return;
    }

    _b_loading = true;
    LoadingDialog* dialog = new LoadingDialog(this);
    dialog->setModal(true);
    dialog->show();
    qDebug() << "run TestAddChatUserList";
    
    // 使用QTimer代替sleep，避免阻塞主线程
    QTimer::singleShot(1000, this, [this, dialog](){
        TestAddChatUserList();
        dialog->deleteLater();
        _b_loading = false;
    });
}

// 测试添加聊天用户列表
void ChatDialog::TestAddChatUserList()
{
    static const std::vector<QString>  strs ={"hello world !",
                                              "nice to meet u",
                                              "New year，new life",
                                              "You have to love yourself",
                                              "My love is written in the wind ever since the whole world is you"};

    static const std::vector<QString> heads = {
        ":/res/head_1.jpg",
        ":/res/head_2.jpg",
        ":/res/head_3.jpg",
        ":/res/head_4.jpg",
        ":/res/head_5.jpg"
    };

    static const std::vector<QString> names = {
        "HanMeiMei",
        "Lily",
        "Ben",
        "Androw",
        "Max",
        "Summer",
        "Candy",
        "Hunter"
    };

    for(int i = 0; i < 10; i++)
    {
        //生成 0 - 99 的随机数
        int index = QRandomGenerator::global()->bounded(100);
        int str_index = index % strs.size();
        int head_index = index % heads.size();
        int name_index = index % names.size();

        auto* chat_user_wid = new ChatUserWid;
        chat_user_wid->SetInfo(names[name_index], heads[head_index], strs[str_index]);
        QListWidgetItem* item = new QListWidgetItem;
        item->setSizeHint(chat_user_wid->SizeHint());
        ui->user_list_Widget->addItem(item);
        ui->user_list_Widget->setItemWidget(item, chat_user_wid);
    }
}
